package com.debao.common.utils;

import com.alibaba.fastjson2.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.amazonaws.util.ImmutableMapParameter;
import com.debao.common.exception.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class AliyunSmsUtil {

    /**
     * 产品名称:云通信短信API产品
     */
    private static final String PRODUCT = "Dysmsapi";

    /**
     * 产品域名
     */
    private static final String DOMAIN = "dysmsapi.aliyuncs.com";

    /**
     * todo 开发者自己的AK(在阿里云访问控制台寻找)
     */
    private static final String ACCESS_KEY_ID = "LTAI5tMw3VrK7nkQ72iZW1rJ";

    /**
     * todo accessKeySecret(在阿里云访问控制台寻找)
     */
    private static final String ACCESS_KEY_SECRET = "xClN1VVq46P0e6spXJLDf500HehZ2C";

    /**
     * todo 必填:短信签名
     */
    private static final String SIGN_NAME = "友德宝";

    /**
     * todo 必填:REGION_ID
     */
    private static final String REGION_ID = "cn-hangzhou";

    private static final String TEMPLATECODE = "";
    private static final Logger log = LoggerFactory.getLogger(AliyunSmsUtil.class);

    /**
     * 发送方法
     *
     * @param phone         电话号码
     * @param templateCode  模板编号
     * @param templateParam 模板中的参数
     * @return 返回值
     * @throws ClientException 异常
     */
    public static SendSmsResponse sendSms(String phone, String templateCode, String templateParam) throws ClientException {
        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile(REGION_ID, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        DefaultProfile.addEndpoint(REGION_ID, REGION_ID, PRODUCT, DOMAIN);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers(phone);
        request.setSignName(SIGN_NAME); // 短信签名
        request.setTemplateCode(templateCode);
        request.setTemplateParam(templateParam);
        //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
        request.setOutId("10000");
        //hint 此处可能会抛出异常，注意catch
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
        log.info("短信接口返回的数据----------------");
        log.info("Code=" + sendSmsResponse.getCode());
        log.info("Message=" + sendSmsResponse.getMessage());
        return sendSmsResponse;
    }

    public static String sendCode(String phone, String code) throws ClientException {

        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile(REGION_ID, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        DefaultProfile.addEndpoint(REGION_ID, REGION_ID, PRODUCT, DOMAIN);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers(phone);
        request.setSignName(SIGN_NAME); // 短信签名
        request.setTemplateCode(TEMPLATECODE);
        request.setTemplateParam(JSONObject.toJSONString(ImmutableMapParameter.of("phone", phone)));
        //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
        request.setOutId("10000");
        //hint 此处可能会抛出异常，注意catch
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);

        if (!sendSmsResponse.getCode().equals("OK")) {
            throw new ServiceException(sendSmsResponse.getMessage());
        }

        return code;
    }


    /**
     * 主方法
     *
     * @param args
     * @throws ClientException
     * @throws InterruptedException
     */
    public static void main(String[] args) throws ClientException, InterruptedException {

        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("name","杭州茶田花语");
        String code = "0002";
        paramMap.put("code", code);
        String templateParam = JSONObject.toJSONString(paramMap);
        String phones = "15357415371";
//        String templateCode = "SMS_208626525";  //模板编号，不同场景，编号不同（如注册，找回密码，密码登录等）
        String templateCode = "SMS_467770059";


        SendSmsResponse response = sendSms(phones, templateCode, templateParam);
        System.out.println("短信接口返回的数据----------------");
        System.out.println("Code=" + response.getCode());
        System.out.println("Message=" + response.getMessage());
        System.out.println("RequestId=" + response.getRequestId());
        System.out.println("BizId=" + response.getBizId());


    }


}
